package com.hbx.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbx.commonutils.R;
import com.hbx.eduservice.entity.EduCourse;
import com.hbx.eduservice.entity.EduTeacher;
import com.hbx.eduservice.service.EduCourseService;
import com.hbx.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author hbx
 * @Date 2023/10/12 21:17
 */
@Api(tags = {"前台首页数据管理"})
@CrossOrigin
@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduTeacherService teacherService;

    //查询前8条热门课程，前4名讲师
    @GetMapping("/index")
    public R index(){
        LambdaQueryWrapper<EduCourse> queryCourse = new LambdaQueryWrapper<>();
        queryCourse.orderByDesc(EduCourse::getViewCount);
        queryCourse.last("limit 8");
        List<EduCourse> courses = courseService.list(queryCourse);

        LambdaQueryWrapper<EduTeacher> queryTeacher = new LambdaQueryWrapper<>();
        queryTeacher.orderByDesc(EduTeacher::getId);
        queryTeacher.last("limit 4");
        List<EduTeacher> teachers = teacherService.list(queryTeacher);
        return R.ok().data("courses",courses).data("teachers",teachers);
    }
}
